home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2695 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  5.7 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c,gnu.gcc.help
  4. Subject: Re: [Q] WEIRD macro bug only when compiling with GCC
  5. Date: 23 Jan 1996 06:19:12 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Jan22231912@qcd.lanl.gov>
  8. References: <DLKABL.KGK@info.physics.utoronto.ca> <TANMOY.96Jan21212916@qcd.lanl.gov>
  9.     <DLM9KM.IoJ@info.physics.utoronto.ca>
  10. NNTP-Posting-Host: qcd.lanl.gov
  11. Mime-Version: 1.0
  12. Content-Type: text
  13. In-reply-to: olivers@helios.physics.utoronto.ca's message of Tue, 23 Jan 1996 04:24:22 GMT
  14.  
  15. --text follows this line--
  16.  
  17. (I mailed this to the poster because he mailed his post to me ... this
  18. copy is for others following the thread. It is already too long, so I
  19. do not quote the original in detail).
  20.  
  21.  
  22. Your mistake is in assuming there is a order among the operands of the
  23. + operation; I had stated (as you correctly quote):
  24.  
  25. >a=c and c*c. However as the + operator does not enforce _any_ order
  26. >(not even the existence of an order) between its two operands, there
  27. >is no sequence point _between_ a=b and a=c.
  28.  
  29. Look at the part in parenthesis! So, what I meant to say (and as has
  30. been discussed quite often in the comp.std.c forum) is that what is
  31. guaranteed is that in (a=b,b*b) + (a=c,c*c) (I choose the easier case
  32. to describe, replace b*b and c*c by a*a consistently throughout and
  33. everything still stands correct):
  34.  
  35. The side effects of a=b will be done before the b*b
  36. The side effects of a=c will be done before the c*c
  37.  
  38. No other statement about side effects can be made! Note that
  39. parentheses enforce absolutely _no_ requirement on side effects: in
  40. particular, there is no requirement that (a=b,b*b) be done as one
  41. unit; before _or_ after (a=c,c*c). In discussions in comp.std.c, this
  42. is called `interleaved' evaluation. Thus a possible order of
  43. evaluation is 
  44.  
  45. a=b
  46. a=c
  47. b*b
  48. c*c
  49.  
  50. Hope that explains the exact meaning of sequence points. Now to
  51. discuss a few more of your statements:
  52.  
  53.  > Right about the sequence points. I could see that this would be a
  54.  > problem if I had something like (a=b) + (a=c).  Then the program sets
  55.  > a=b, but right after sets a to c, so it would yield 2*c (or 2*b if the
  56.  > compiler first evaluated the right hand side of the + operator).  *But*
  57.  
  58. This is wrong! It could equally well yield zero or 42 as well. The
  59. behaviour is called `undefined' which means that the standard does not
  60. put any constraints on the compiler trying to compile your
  61. program. Imagine that a hardware needs 2 stores to store an int
  62. (e.g. when the data bus is 8bit wide and ints are 16). It could chose
  63. to store the bytes and evaluate in the following order
  64.  
  65. low  order byte of b into low  order byte of a
  66. low  order byte of c into low  order byte of a
  67. high order byte of b into high order byte of a
  68. high order byte of c into high order byte of a
  69. evaluate a+a in the standard fashion
  70.  
  71. Note that now you get neither 2*c nor 2*b. Such optimizations were
  72. meant to be allowed, and so when the standard says somethig is
  73. undefined, treat it _precisely_ as that. In fact, on exotic enough
  74. hardware, the program can core dump on a signal of simultaneous memory
  75. update signal (made up example).
  76.  
  77.  > here, the result a*a must be stored in some place independent from a,
  78.  > say a register or some memory location.  The 2 sides of the + operator
  79.  > are evaluated independently (ie there is no precedence problem between
  80.  > the + and = and comma), and the comma *is* a sequence point with forced
  81.  > order from left to right.  Therefore when the program evaluates (a=b,
  82.  > a*a) (or the other side of the + operator just revert the conclusion
  83.  > which follows), if first evaluates b, stores it in a, then does a*a and
  84.  > stores the result somewhere in memory.  Then it evaluates c, stores it
  85.  > in a (even if a is now changed, it doesn't change the first a*a, since
  86.  > that is a stored result somewhere in memory), evaluates a*a, and should
  87.  > store it somewhere *else* in memory, then adds the contents of those 2
  88.  > memory locations.  So, there should be no problem.  There is no
  89.  
  90. As I mentioned before, you are assuming that either the left or the
  91. right side of the + is evaluated in its entirety before the other side
  92. starts. There is no reason why this must be so. (For example, if the
  93. compiler knows that you have two cpus it can use one to calculate the
  94. lhs of + and the other to calculate the rhs. Where will the ordering
  95. come from?)
  96.  
  97.  > ambiguity about say, doing first a=b, then a=c, and only after that
  98.  > doing a*a and again a*a, because of the parentheses.  Or, why would GCC
  99.  > store the two results (from each side of the + operator) in the same
  100.  > memory location, just because a*a occurs on both sides of the + operand?
  101.  > 
  102.  
  103. It does not.
  104.  
  105.  > >problem mentioned above, but in addition there is no sequence point
  106.  > >between the a=c and the first a*a (likewise between the a=b and the
  107.  > >second a*a). 
  108.  > 
  109.  > You must mean something else here, since you just said (and I checked),
  110.  > that the comma is a sequence point, and not only that, its order is
  111.  > specified.
  112.  > 
  113.  
  114. The comma is a sequence point, but only between _its_ first and second
  115. operands. It says that its left operand is evaluated completely (along
  116. with side effects) _before_ its right operand: it does _not_ say that
  117. nothing else can happen in the middle.
  118.  
  119. Cheers
  120. Tanmoy
  121. --
  122. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  123. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  124. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  125. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  126. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  127. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  128.